
//  BVShopCart.m
//  BuyVegetables1
//
//  Created by 叶思超 on 15/10/26.
//  Copyright (c) 2015年 叶思超. All rights reserved.
//

#import "BVShopCart.h"
#import "BVShopsLeftCategoryDetail.h"
#import "FMDatabase.h"
#import "MJExtension.h"

@implementation BVShopCart
static FMDatabase *_db;

/**
 *  初始化
 */
+ (void)initialize {
    // 获取存储路径
    NSString *pathFile = [[NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"shopDetail.sqlite"];
    
    // 打开数据库
    _db = [FMDatabase databaseWithPath:pathFile];
    
    // 打开失败
    if (![_db open]) return;
    
    // 创建表
    [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_shopCart(id integer PRIMARY KEY, shopDetail blob NOT NULL, shopDetail_id text NOT NULL, shopDetail_count text NOT NULL);"];
}

/**
 *  添加一个商品到购物车
 */
+ (void)addShopToCart:(BVShopsLeftCategoryDetail *)shopDetail {
    // 将数据转换成二进制流
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:shopDetail];
    [_db executeUpdateWithFormat:@"INSERT INTO t_shopCart(shopDetail, shopDetail_id, shopDetail_count) VALUES(%@, %@, %@);", data, shopDetail.ID, [NSString stringWithFormat:@"%ld", [BVShopCart shopCartCounts:shopDetail] + 1]];
    
}

/**
 *  从购物车删除一个商品
 */
+ (void)deleteShopFromCart:(BVShopsLeftCategoryDetail *)shopDetail {
    [_db executeUpdateWithFormat:@"DELETE FROM t_shopCart WHERE shopDetail_id = %@;", shopDetail.ID];
}

/**
 *  修改已存在购物车的商品数量
 *
 *  @param shopDetail 那个商品
 *  @param count      修改的数量
 */
+ (void)modifyShopToCartL:(BVShopsLeftCategoryDetail *)shopDetail count:(NSInteger) count {
    BOOL res = [_db executeUpdateWithFormat:@"UPDATE t_shopCart SET  shopDetail_count = %@ WHERE shopDetail_id = %@;", [NSString stringWithFormat:@"%ld", (long)count],shopDetail.ID];
    if (!res) {
        NSLog(@"error when update db table");
    } else {
        NSLog(@"success to update db table");
    }
}

/**
 *  返回购物车商品个数,page从1开始
 */
+ (NSArray *)shopsCount:(NSInteger)page {
    NSInteger size = 100;
    NSInteger pos = (page - 1) * size;
    FMResultSet *result = [_db executeQueryWithFormat:@"SELECT * FROM t_shopCart ORDER BY id DESC LIMIT %ld,%ld;", (long)pos, (long)size];
    NSMutableArray *detailShops = [NSMutableArray array];
    while (result.next) {
        BVShopsLeftCategoryDetail *detail = [NSKeyedUnarchiver unarchiveObjectWithData:[result objectForColumnName:@"shopDetail"]];
        [detailShops addObject:detail];
    }
    return detailShops;
}

/**
 *  购物车商品的数量
 */
+ (NSInteger)shopCartCounts:(BVShopsLeftCategoryDetail *)shopDetail {
    FMResultSet *result = [_db executeQueryWithFormat:@"SELECT * FROM t_shopCart;"];
    BVShopsLeftCategoryDetail *detail;
    while (result.next) {
        detail = [NSKeyedUnarchiver unarchiveObjectWithData:[result objectForColumnName:@"shopDetail"]];
        
        if ([detail.ID isEqualToString:shopDetail.ID]) {
            return  [result intForColumn:@"shopDetail_count"];
        }
    }

    return 0;
}

/**
 *  根据商品ID查找购物车商品
 */
+ (BVShopsLeftCategoryDetail *)lookUpShop:(BVShopsLeftCategoryDetail *)shopDetail {
    FMResultSet *result = [_db executeQueryWithFormat:@"SELECT * FROM t_shopCart;"];
    
    BVShopsLeftCategoryDetail *detail;
    while (result.next) {
        detail = [NSKeyedUnarchiver unarchiveObjectWithData:[result objectForColumnName:@"shopDetail"]];
        
        if ([detail.ID isEqualToString:shopDetail.ID]) {
            return detail;
        }
    }
    return nil;
}

+ (void)deleteAllShopFromCart{
    [_db executeUpdateWithFormat:@"DELETE FROM t_shopCart"];
}
@end
